Spring boot and Thyemeaf 条件与循环
在众多语言之中,都有条件和循环等三个非常重要的语句分别为if、switch、while等三个语句,分别根据不同场景而运用,本次我们将会一一进行列举,也作为我们的Thymeleaf结尾篇(因为我们主打的还是Spring boot,不是Thymeleaf,Thymeleaf只是作为课外部分)
if
布尔也是开发语言三件套之一,通常布尔用于判断和各种循环条件之中,本书将会使用Thymeleaf模板引擎来实现布尔的一系列操作:
在Thymeleaf模板引擎之中,布尔的写法是有区别的如**<div th:if=“${data} == ‘thymeleaf’”<div th:if=”${data=’Thymeleaf’”**,两种写法其中第一种写法即布尔在大阔号外的即将会由Thymeleaf负责处理,如果将布尔写在大阔号内将会由OGNL/SpringEL引擎负责处理:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| <!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>Home</title> <link th:href="@{/css/style.css}" href="../static/css/style.css" rel="stylesheet" type="text/css" /> </head> <body> <h5>Java Thymeleaf boolean</h5> <hr /> <div th:if="${data}=='Thymeleaf'"> <p>Thymeleaf....if $data 变量正确</p> </div> </body> </html>
|
if and unless
本书通过if and unless来实现判断data变量是否为空作为案例,如data变量为空则输出不为空等信息,实现方法如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| <!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>Home</title> <link th:href="@{/css/style.css}" href="../static/css/style.css" rel="stylesheet" type="text/css" /> </head> <body> <h5>Java Thymeleaf boolean</h5> <hr /> <div th:if="${data} == true"> <p>The true urlController $data</p> </div> <div th:unless="${data} == true"> <p>The false urlController $data</p> </div>
</body> </html>
|
在Thymeleaf之中,th:if与th:unless的意思和功能是不同的,通常为th:is是条件成立时执行的内容而th:unless则是条件不成立时执行的内容
switch
switch语法表达式可有效的等效于if或是unless等Thymeleaf语法表达式,来有效且有条件的显示某一条内容:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| <!-- index.html --> <!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>Home</title> <link th:href="@{/css/style.css}" href="../static/css/style.css" rel="stylesheet" type="text/css" /> </head> <body> <h5>Java Thymeleaf switch</h5> <hr /> <div th:switch="${data.name}"> <p th:case="'kunlunsiqu'">My name is kunlunsiqu</p> <p th:case="'kun'">My name is kun</p> </div> </body> </html>
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| package com.example.demo;
import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.GetMapping;
@Controller public class urlController { @GetMapping("index") public String getindex(Model model) { data dataone = new data("kun",20); model.addAttribute("data",dataone); return "index"; } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| package com.example.demo;
public class data { private String name; private int age;
public data(String name, int age) { this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } }
|
switch + case
上面我们介绍了switch单个的执行效果,当然我们虽然也写了 th:case但如果条件都不构成时我们需要输出什么呢?这个问题在Thymeleaf已经给了我们答案:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| <!-- index.html --> <!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>Home</title> <link th:href="@{/css/style.css}" href="../static/css/style.css" rel="stylesheet" type="text/css" /> </head> <body> <h5>Java Thymeleaf switch</h5> <hr /> <div th:switch="${data.name}"> <p th:case="'kunlunsiqu'">My name is kunlunsiqu</p> <p th:case="'kun'">My name is kun</p> <p th:case="*">My name is no kunlunsiqu and kun!</p> </div> </body> </html>
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| package com.example.demo;
import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.GetMapping;
@Controller public class urlController { @GetMapping("index") public String getindex(Model model) { data dataone = new data("jujingyi",20); model.addAttribute("data",dataone); return "index"; } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| package com.example.demo;
public class data { private String name; private int age;
public data(String name, int age) { this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } }
|
switch与case相互使用的时候本身并不会对当条件都不构成时输出信息,而如果将case条件更改为"**"时将会在当条件都不构成时输出指定信息,当然,不仅仅可以使用case来作为当条件都不构成时输出的信息,通过阅读上一章节读者们可会有更多的方案来代替 th:case=”*” 语法。
each
本书我们通过使用列表来进行循环的实现,其主要由Controller(控制器)和th:text、th:each来分别进行实现如:
WebController.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| package com.example.demo;
import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import java.util.Arrays; import java.util.Map;
@Controller public class WebController { @RequestMapping("/index") public String index(Map<String,Object> map) { map.put("data",Arrays.asList("one","two","three")); return "index"; } }
|
index.html
1 2 3 4 5 6 7 8 9 10 11 12
| <!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>Index</title> </head> <body> <h5>Java Thymeleaf th:each</h5> <hr/> <p th:text="${each}" th:each="each: ${data}"></p> </body> </html>
|
application.propertion
1
| spring.thymeleaf.cache=false
|
⬅️ Go back